<template>
  <div id="abc" class="charts_container" ref="echartsPoly"></div>
</template>

<script>
export default {
  name: 'potLiquidChart',
  data () {
    return {
      chartInstance: null,
      initOption: null
    }
  },
  created () {},
  mounted () {
    this.initChart()
  },
  props: {
    chartData: {
      type: Array,
      required: true
    }
  },
  watch: {
    chartData: {
      handler (newVal, oldVal) {
        // console.log('new', newVal, oldVal)
        // if (oldVal !== newVal && oldVal !== undefined) {
        if (oldVal !== newVal && newVal.length !== 0) {
          this.realData = newVal
          var copys = JSON.parse(JSON.stringify(this.realData))
          this.timeWay(copys)
          const seriesGroup = []
          seriesGroup[0] = {
            name: copys[0][1].slice(0, 3) + '温度',
            type: 'line',
            xAxisIndex: 0,
            yAxisIndex: 0,
            smooth: true,
            showAllSymbol: false,
            symbol: 'circle',
            // barWidth: 4,
            itemStyle: {
              normal: {
                color: 'rgb(0,207,255)'
              }
            },
            encode: {
              x: 0,
              y: 1
            }
          }
          seriesGroup[1] = {
            name: copys[0][1].slice(0, 3) + '液位',
            type: 'line',
            smooth: true,
            showAllSymbol: false,
            symbol: 'circle',
            symbolSize: 3,
            xAxisIndex: 0,
            yAxisIndex: 1,
            itemStyle: {
              normal: {
                color: 'rgb(0,108,237)'
              }
            },
            encode: {
              x: 0,
              y: 2
            }
          }
          this.updateChart(seriesGroup, copys, copys[0][1].slice(0, 3))
        }
      },
      immediate: true
    }
  },
  beforeDestroy () {
    window.removeEventListener('resize', () => {
      this.chartInstance.resize()
    })
  },
  methods: {
    initChart () {
      this.initOption = {
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(255,255,255,0.1)',
          axisPointer: {
            type: 'shadow',
            label: {
              show: true,
              backgroundColor: '#7B7DDC'
            }
          }
        },
        legend: {
          data: ['洁净罐液位', '洁净罐温度'],
          textStyle: {
            color: '#B4B4B4'
          },
          top: '0%'
        },
        grid: {
          x: '8%',
          width: '81%',
          y: '12%',
          bottom: '13%',
          right: '6%',
          top: '19%'
        },
        xAxis: {
          type: 'category',
          axisLine: {
            lineStyle: {
              color: 'rgb(154, 170, 203)'
            }
          },
          nameTextStyle: {
            color: 'rgb(154, 170, 203)'
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            interval: 2,
            color: 'rgb(154, 170, 203)',
            formatter: function (value) {
              let ret = '' // 拼接加\n返回的类目项
              var maxLength = 6 // 每行最多个数
              var valLength = value.length // x轴一个项的总数
              var rowN = Math.ceil(valLength / maxLength)
              if (rowN > 1) { // 如果大于5个字符
                for (let i = 0; i < rowN; i++) {
                  let temp = ''
                  temp = value.substring(i * maxLength, i * maxLength + maxLength) + '\n'
                  ret += temp
                }
                return ret
              } else {
                return value
              }
            },
            textStyle: {
              fontSize: 8
            }
          }
          // data: category
        },
        yAxis: [
          {
            name: '液位',
            type: 'value',
            splitLine: { show: false },
            axisLine: {
              lineStyle: {
                // color: 'rgb(154, 170, 203)'
              }
            },
            nameTextStyle: {
              color: 'rgb(154, 170, 203)'
            },
            axisTick: { // 不显示轴刻度
              show: false
            },
            axisLabel: {
              color: 'rgb(154, 170, 203)',
              formatter: '{value}'
            }
          },
          {
            name: '温度',
            type: 'value',
            splitLine: { show: false },
            axisLine: {
              lineStyle: {
                // color: 'rgb(154, 170, 203)'
              }
            },
            nameTextStyle: {
              color: 'rgb(154, 170, 203)'
            },
            axisTick: { // 不显示轴刻度
              show: false
            },
            axisLabel: {
              formatter: '{value}',
              color: 'rgb(154, 170, 203)'
            }
          }
        ],
        series: [],
        dataset: {
          source: []
        }
      }
      this.chartInstance = this.$echarts.init(this.$refs.echartsPoly)
      // console.log('温度曲线initOption', this.initOption)
      this.chartInstance.setOption(this.initOption)
      this.echartsResize()
    },
    updateChart (seriesGroup, realData, names) {
      const dataOption = {
        series: seriesGroup,
        legend: {
          data: [`${names}液位`, `${names}温度`]
        },
        dataset: {
          source: realData // 数组
        }
      }
      this.chartInstance.setOption(dataOption)
    },
    echartsResize () {
      window.addEventListener('resize', () => {
        this.chartInstance.resize()
      })
    },
    timeWay (arrBig) {
      if (arrBig[1][0].length === 19) {
        for (var i = 1; i < arrBig.length; i++) {
          arrBig[i][0] = arrBig[i][0].substring(5, arrBig[i][0].length - 3)
        }
      }
    }
  }
}
</script>

<style  scoped>
.charts_container {
  width: 100%;
  height: 20vh
}
</style>
